package com.irdstudio.efp.nls.service.impl.yx;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.efp.console.common.IrAdjModeEnum;
import com.irdstudio.efp.console.common.RateTypeEnum;
import com.irdstudio.efp.console.service.facade.PrdInfoService;
import com.irdstudio.efp.console.service.vo.PrdInfoVO;
import com.irdstudio.efp.ctr.common.ContConstant;
import com.irdstudio.efp.ctr.service.facade.CtrLoanContService;
import com.irdstudio.efp.ctr.service.vo.CtrLoanContVO;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import com.irdstudio.efp.nls.service.facade.NlsApplyInfoService;
import com.irdstudio.efp.nls.service.impl.SoltServiceImpl;
import com.irdstudio.efp.nls.service.vo.NlsApplyInfoVO;
import com.irdstudio.efp.nls.service.vo.queue.NlsProcessBizVO;


/**
 * 合同生成信息
  * @author heyingfeng
  * @since 2019年3月20日 下午4:30:11
  * @version 1.0
 */
@Service("letterCtrService")
public class LetterCtrServiceImpl extends SoltServiceImpl {

	private static final Logger log = LoggerFactory.getLogger(LetterCtrServiceImpl.class);
	private static final DateTimeFormatter timeStrFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

	@Override
	public void doExecute(NlsProcessBizVO nlsProcessBizVo,NlsApplyInfoVO nlsApplyInfoVO) throws Exception {
		String applySeq = nlsProcessBizVo.getApplySeq();
		try {
			log.info("开始生成合同信息,流水号:" + applySeq);
			Objects.requireNonNull(nlsApplyInfoVO);

			String prdId = Objects.requireNonNull(nlsApplyInfoVO.getPrdId());
			PrdInfoVO prdInfoVo = new PrdInfoVO();
			prdInfoVo.setPrdId(prdId);
			prdInfoVo = prdInfoService.queryByPk(prdInfoVo);
			/** 插入贷款合同表 **/
			final CtrLoanContVO ctrLoanContVO = new CtrLoanContVO();
			ctrLoanContVO.setContNo(applySeq);
			ctrLoanContVO.setSerno(applySeq);
			ctrLoanContVO.setContType(ContConstant.CMIS_PRD_TYPE_02); // 合同类型 -- 对私贷款
			ctrLoanContVO.setPrdCode(prdInfoVo.getPrdCode());
			ctrLoanContVO.setPrdId(prdInfoVo.getPrdId());
			ctrLoanContVO.setPrdName(prdInfoVo.getPrdName());
			ctrLoanContVO.setCusId(nlsApplyInfoVO.getCusId());
			ctrLoanContVO.setCusName(nlsApplyInfoVO.getCusName());
			ctrLoanContVO.setCertCode(nlsApplyInfoVO.getCertCode());
			ctrLoanContVO.setCertType(nlsApplyInfoVO.getCertType());
			ctrLoanContVO.setTermTimeType(nlsApplyInfoVO.getLoanTermType()); // 期限类型
			ctrLoanContVO.setLoanTerm(new BigDecimal(nlsApplyInfoVO.getLoanTerm()));
			ctrLoanContVO.setApprovalAmt(nlsApplyInfoVO.getApplyAmt()); // 审批金额
			ctrLoanContVO.setContAmt(nlsApplyInfoVO.getApplyAmt()); // 合同金额
			ctrLoanContVO.setAvailAmt(BigDecimal.ZERO); // 合同可用金额
			ctrLoanContVO.setTotalIssueAmt(nlsApplyInfoVO.getApplyAmt()); // 累计发放金额
			ctrLoanContVO.setContState(ContConstant.CONT_STATUS_101); // 合同状态 - 待签订
			ctrLoanContVO.setContStartDt(LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE)); 
			ctrLoanContVO.setContEndDt(LocalDate.now()  
					.plusMonths(Long.valueOf(nlsApplyInfoVO.getLoanTerm()))
					.format(DateTimeFormatter.ISO_LOCAL_DATE));
			ctrLoanContVO.setIntRateType(RateTypeEnum.NORMAL.getCode()); // 普通贷款
			ctrLoanContVO.setIrAdjustMode(IrAdjModeEnum.FIXED.getCode()); // 固定利率
			ctrLoanContVO.setUseDec(nlsApplyInfoVO.getLoanPurpose()); // 借款用途
			ctrLoanContVO.setInputBrId(ESBClientConstance.InstId);
			ctrLoanContVO.setMainBrId(ESBClientConstance.InstId);
			ctrLoanContVO.setFinaBrId(ESBClientConstance.InstId);
			ctrLoanContVO.setLegalOrgCode(ESBClientConstance.Company);
			ctrLoanContVO.setLegalOrgName("广州银行智慧银行"); 
			ctrLoanContVO.setChannelCode(nlsApplyInfoVO.getChannelNo());
			ctrLoanContVO.setLastUpdateUser("admin");
			ctrLoanContVO.setCreateUser("admin");
			ctrLoanContVO.setCreateTime(LocalDateTime.now().format(timeStrFormatter));
			ctrLoanContVO.setLastUpdateTime(LocalDateTime.now().format(timeStrFormatter));
			ctrLoanContVO.setLastUpdateUser("admin");
			int num2 = ctrLoanContService.insertCtrLoanCont(ctrLoanContVO);
			if(num2 == -1) {
				throw new BizException("生成合同信息失败！");
			}
		} catch (Exception e) {
			log.error(Arrays.toString(e.getStackTrace()));
			nlsProcessBizVo.setExceptionFlag(true);
			nlsProcessBizVo.setExceptionMsg(e);
		}
	}

	@Autowired
	@Qualifier("nlsApplyInfoService")
	private NlsApplyInfoService nlsApplyInfoService;
	@Autowired
	@Qualifier("prdInfoService")
	private PrdInfoService prdInfoService;
	@Autowired
	@Qualifier("ctrLoanContService")
	private CtrLoanContService ctrLoanContService;

}
